package com.ynny.practice10.dao;

import com.ynny.practice10.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository

public class UserDaoImpl implements UserDao{

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public User selectUserName(String userName) {
       String sql = "select * from user where username = ?";
      List<User> list = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(User.class),userName);

      if (list.size()==0){
          return null;
      }
       return list.get(0);
    }

    @Override
    public int insertUser(User user) {

        String sql = "insert into user (username,password,identity) values(?,?,?)";
        int i = jdbcTemplate.update(sql,
                user.getUsername(),
                user.getPassword(),
                user.getIdentity()
                );
        return i;

    }
}
